<!DOCTYPE html>
<html>

<head>
  <meta charset="utf-8" />
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
  <meta name="renderer" content="webkit" />
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" />
  <title>千锋教育大前端React教学</title>
  <!-- 引入react -->
  <script src="../../lib/js/react.js"></script>
  <!-- 引入react-dom -->
  <script src="../../lib/js/react-dom.js"></script>
  <!-- 引入babel 用来解析jsx -->
  <script src="../../lib/js/babel.js"></script>
</head>

<body style="margin:100px;font-size:22px;">
  <!-- 定义一个容器 -->
  <div id="app"></div>
  <script type="text/babel"> /*此处一定要写babel*/
    const { createContext, useState, useContext } = React; // 声明 useState
    const SurnameContext = createContext({ surname: '孙' }); // 参数是默认值
    function Parent() {
      const context = useContext(SurnameContext);
      return (
        <div>
          <div>我是第一代：</div>
          <div>我姓：{context.surname}</div>
          <Child name="朱大肠" />
          <Child name="朱小明" />
        </div>
      )
    }
    function Child(props) {
      const context = useContext(SurnameContext);
      return (
        <div>
          <div>我是第二代：</div>
          <div>我叫：{props.name}、我姓：{context.surname}</div>
        </div>
      )
    }
    function App() {
      return (
        <SurnameContext.Provider value={{ /*相当于提供了一个全局变量，在所有子节点都可以用useContext获取到*/
          surname: '朱', // 姓
        }}>
          <Parent />
        </SurnameContext.Provider>
      );
    }
    // 挂载应用
    ReactDOM.createRoot(document.getElementById('app')).render(<App />);
  </script>
</body>

</html>